/* $Id: SingleClientPersonDomainObjectManagerImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.domain.manager.provider;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.dao.SingleClientPersonDAO;
import ae.pcfc.etks.online.common.dao.SingleClientPersonRoleDAO;
import ae.pcfc.etks.online.common.domain.ClientPersonReq;
import ae.pcfc.etks.online.common.domain.ClientPersonRoleReq;
import ae.pcfc.etks.online.common.domain.TTscClientCategory;
import ae.pcfc.etks.online.common.domain.TTscClientPerson;
import ae.pcfc.etks.online.common.domain.TTscLicType;
import ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager;
import ae.pcfc.etks.online.common.exception.LookupException;
import ae.pcfc.etks.online.common.exception.PersistenceException;
import ae.pcfc.etks.online.common.vo.ClientNameVO;
import ae.pcfc.etks.online.common.vo.SCClientPersonRoleVO;
import ae.pcfc.etks.online.common.vo.SearchCriteriaPersonVO;
import ae.pcfc.etks.online.common.vo.TrakheesIdAndPINVO;
import ae.pcfc.etks.online.common.vo.TrakheesIdVO;

public class SingleClientPersonDomainObjectManagerImpl implements SingleClientPersonDomainObjectManager {

	private SingleClientPersonDAO singleClientPersonDAO;
	private SingleClientPersonRoleDAO singleClientPersonRoleDAO;

	/**
	 * setSingleClientPersonDAO
	 * 
	 * @param singleClientPersonDAO
	 * void
	 */
	public void setSingleClientPersonDAO(SingleClientPersonDAO singleClientPersonDAO) {
		this.singleClientPersonDAO = singleClientPersonDAO;
	}

	/**
	 * setSingleClientPersonRoleDAO
	 * 
	 * @param singleClientPersonRoleDAO
	 * void
	 */
	public void setSingleClientPersonRoleDAO(SingleClientPersonRoleDAO singleClientPersonRoleDAO) {
		this.singleClientPersonRoleDAO = singleClientPersonRoleDAO;
	}

	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.BaseDomainObjectManager#add(java.lang.Object)
	 * 
	 */
	public <T> void add(T obj) throws PersistenceException {
		// TODO Auto-generated method stub

	}

	/**
	 * update
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.BaseDomainObjectManager#update(java.lang.Object)
	 * 
	 */
	public <T> void update(T obj) throws PersistenceException {
		// TODO Auto-generated method stub

	}

	/**
	 * delete
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.BaseDomainObjectManager#delete(java.lang.Object)
	 * 
	 */
	public <T> void delete(T obj) throws PersistenceException {
		// TODO Auto-generated method stub

	}

	/**
	 * findByPrimaryKey
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.BaseDomainObjectManager#findByPrimaryKey(java.lang.String)
	 * 
	 */
	public <T> T findByPrimaryKey(String id) throws LookupException {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * findAll
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.BaseDomainObjectManager#findAll()
	 * 
	 */
	public <T> Collection<T> findAll() throws LookupException {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager#add(ae.pcfc.etks.online.common.domain.ClientPersonReq)
	 * 
	 */
	public Map<String, String> add(ClientPersonReq clientPersonReq) throws PersistenceException {

		String trakheesId = singleClientPersonDAO.add(clientPersonReq);

		// Inserting the Roles in the TSC
		/*for (ClientPersonRoleReq personRole : clientPersonReq.getClientPersonRoleReqs()) {
			this.singleClientPersonRoleDAO.addOrUpdate(new Long(personRole.getCsrReqId()), trakheesId, clientPersonReq.getCpCreatedUser(), ConstantsCommon.ACTIVE);
		}*/

		String pinCode = this.singleClientPersonDAO.getPinCodeByTrakheesId(trakheesId);

		Map<String, String> values = new HashMap<String, String>();
		values.put(ConstantsCommon.TRAKHEES_ID, trakheesId);
		values.put(ConstantsCommon.PIN_CODE, pinCode);

		return values;
	}

	/**
	 * validateTrakheesIdAndPIN
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager#validateTrakheesIdAndPIN(ae.pcfc.etks.online.common.vo.TrakheesIdAndPINVO)
	 * 
	 */
	public boolean validateTrakheesIdAndPIN(TrakheesIdAndPINVO idAndPINVO) {
		return this.singleClientPersonDAO.validateTrakheesIdAndPIN(idAndPINVO);
	}

	/**
	 * findSCPersonByTrakheesId
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager#findSCPersonByTrakheesId(java.lang.String)
	 * 
	 */
	public TTscClientPerson findSCPersonByTrakheesId(String trakheesId) {
		return this.singleClientPersonDAO.findSCPersonByTrakheesId(trakheesId);
	}

	/**
	 * findSCPersonRoleByTrakheesId
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager#findSCPersonRoleByTrakheesId(java.lang.String)
	 * 
	 */
	public List<SCClientPersonRoleVO> findSCPersonRoleByTrakheesId(String trakheesId) {

		return this.singleClientPersonRoleDAO.findSCPersonRoleByTrakheesId(trakheesId);

	}

	/**
	 * addSCPersonRole
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager#addSCPersonRole(java.lang.Long, java.lang.String, java.lang.String)
	 * 
	 */
	public void addSCPersonRole(Long roleId, String trakheesId, String strUserName) {

		this.singleClientPersonRoleDAO.addOrUpdate(roleId, trakheesId, strUserName, ConstantsCommon.CLIENT_ROLE_STATUS_ACTIVE);

	}

	/**
	 * findSCTrakheesIdsByRoleId
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager#findSCTrakheesIdsByRoleId(java.lang.String)
	 * 
	 */
	public List<TrakheesIdVO> findSCTrakheesIdsByRoleId(String roleId) {

		return this.singleClientPersonDAO.findSCTrakheesIdsByRoleId(roleId);

	}

	/**
	 * validateClientName
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager#validateClientName(ae.pcfc.etks.online.common.vo.ClientNameVO)
	 * 
	 */
	public boolean validateClientName(ClientNameVO clientNameVO) {
		return this.singleClientPersonDAO.validateClientName(clientNameVO);
	}

	/**
	 * findTrakheesIdAndPinByEmail
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager#findTrakheesIdAndPinByEmail(java.lang.String)
	 * 
	 */
	public TrakheesIdAndPINVO findTrakheesIdAndPinByEmail(String email) {

		return this.singleClientPersonDAO.findTrkheesIdAndPinByEmail(email);
	}

	/**
	 * getClientCategory
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager#getClientCategory(java.lang.String)
	 * 
	 */
	public List<TTscClientCategory> getClientCategory(String clientType) throws LookupException {
		
		return this.singleClientPersonDAO.getClientCategory(clientType);
	}

	/**
	 * getClientCategoryById
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager#getClientCategoryById(java.lang.String)
	 * 
	 */
	public TTscClientCategory getClientCategoryById(String categoryId) throws LookupException {
		
		return this.singleClientPersonDAO.getClientCategoryById(categoryId);
	}

	/**
	 * getClientLicenseTypeByCode
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager#getClientLicenseTypeByCode(java.lang.String)
	 * 
	 */
	public TTscLicType getClientLicenseTypeByCode(String licenseTypeCode) throws LookupException {
		
		return this.singleClientPersonDAO.getClientLicenseTypeByCode(licenseTypeCode);
	}
	/**
	 * searchPerson
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientPersonDomainObjectManager#searchPerson(ae.pcfc.etks.online.common.vo.SearchCriteriaPersonVO)
	 * 
	 */
	public List<TTscClientPerson> searchPerson(SearchCriteriaPersonVO searchCriteriaPersonVO) {
		return this.singleClientPersonDAO.searchPerson(searchCriteriaPersonVO);
	}

	public void removeSCPersonRole(Long roleId, String cTrakheesId, String strUserName) {
		this.singleClientPersonRoleDAO.addOrUpdate(roleId, cTrakheesId, strUserName, ConstantsCommon.CLIENT_ROLE_STATUS_INACTIVE);
	}

	public void undoCreatePerson(ClientPersonReq clientPersonReq,String strTrakheesId, String clientStatus) {
		if(clientPersonReq != null){
			// Disabling the Roles in the TSC
			if(clientPersonReq.getClientPersonRoleReqs() != null){
				for (ClientPersonRoleReq personRole : clientPersonReq.getClientPersonRoleReqs()) {
					this.singleClientPersonRoleDAO.addOrUpdate(new Long(personRole.getCsrReqId()), strTrakheesId, clientPersonReq.getCpCreatedUser(), ConstantsCommon.CLIENT_ROLE_STATUS_INACTIVE);
				}
			}
			String rows = singleClientPersonDAO.updateClientPersonStatus(clientPersonReq, strTrakheesId, clientStatus);			
		}
	}
}
